﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using NET.Tools;
using Puzzler.Plugin;
using Puzzler.Plugin.Interface;
using Puzzler.Project;

namespace Puzzler.Word.GUI
{
    /// <summary>
    /// Interaktionslogik für PuzzleChoiceDialog.xaml
    /// </summary>
    public partial class PuzzleChoiceDialog : Window
    {
        public static IPuzzlePlugin ShowPuzzleChoiceDialog()
        {
            PuzzleChoiceDialog dlg = new PuzzleChoiceDialog();
            if (dlg.ShowDialog().GetValueOrDefault(false))
            {
                return (IPuzzlePlugin) dlg.lvPuzzles.SelectedItem;
            }

            return null;
        }

        private PuzzleChoiceDialog()
        {
            InitializeComponent();
        }

        private void cmbPuzzleCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.RunAfterLoad(() =>
                {
                    cmbPuzzleTypes.Items.Clear();

                    foreach (PluginObject pluginObject in PuzzlerProjectService.Instances.Keys)
                    {
                        if (pluginObject.PuzzleInfo.Category ==
                            (PuzzleCategory)cmbPuzzleCategories.SelectedItem)
                        {
                            cmbPuzzleTypes.Items.Add(pluginObject);
                        }
                    }

                    if (cmbPuzzleTypes.HasItems)
                        cmbPuzzleTypes.SelectedIndex = 0;
                });
        }

        private void cmbPuzzleTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!IsLoaded)
                return;

            lvPuzzles.Items.Clear();

            foreach (PluginObject pluginObject in PuzzlerProjectService.Instances.Keys)
            {
                if (pluginObject.Equals(cmbPuzzleTypes.SelectedItem))
                {
                    ObservableCollection<IPuzzlePlugin> collection = PuzzlerProjectService.Instances[pluginObject];
                    foreach (IPuzzlePlugin puzzlePlugin in collection)
                    {
                        lvPuzzles.Items.Add(puzzlePlugin);
                    }
                }
            }
        }
    }
}
